<?php

namespace App\Http\Controllers\Backend\Role;

use Illuminate\Http\Request;
use App\Http\Models\Role\RoleFrom;
use App\Http\Models\Role\RoleService;
use App\Http\Controllers\Backend\BackendController;

/*
 * RoleController  角色操作类
 *
 * index() 角色列表
 * show() 角色查看
 * create() 渲染角色新增模板
 * store() 角色新增
 * edit() 渲染角色修改模板
 * update() 角色修改
 * destroy() 删除角色
 *
 * @author Jinfeihu
 * 
 * */

class RoleController extends BackendController {

    private $roleService;
    private $request;

    public function __construct(RoleService $roleService, Request $request) {
        $this->roleService = $roleService;
        $this->request = $request;
    }

    /**
     * 角色列表
     * * */
    public function index() {
        $roleDatas = $this->roleService->index($this->request);
        return view('backend.role.index', [
            'roleDatas' => $roleDatas,
            'request' => $this->request
        ]);
    }

    /**
     * 渲染角色新增模板
     * * */
    public function create() {
        $permissionDatas = $this->roleService->getPermissionAll();
        return view("backend.role.create", [
            'permissionDatas' => $permissionDatas
        ]);
    }

    /**
     * 新增角色
     */
    public function store(RoleFrom $roleFrom) {
        $this->roleService->store($roleFrom);
    }

    /**
     * 角色查看
     * * */
    public function show($id) {
        $roleDatas = $this->roleService->show($id);
        return view("backend.role.show", [
            'roleDatas' => $roleDatas,
        ]);
    }

    /**
     * 渲染修改角色模板
     * * */
    public function edit($id) {
        $roleDatas = $this->roleService->show($id);
        $permissionAlls = $this->roleService->getPermissionAll();
        return view('backend.role.update', [
            'roleDatas' => $roleDatas,
            'permissionAlls' => $permissionAlls
        ]);
    }

    /**
     * 修改角色
     * * */
    public function update(RoleFrom $roleFrom, $id) {
        $this->roleService->updateSave($roleFrom, $id);
    }

    /**
     * 角色删除
     * * */
    public function destroy($id) {
        return $this->roleService->remove($id);
    }

    public function __destruct() {
        unset($this->roleService);
        unset($this->request);
    }

}
